/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.babeuf.form.conditions 
{
	import net.babeuf.form.fields.AbstractField;
	import net.babeuf.security.ICaptcha;
	
	/**
	 * MatchCaptchaCondition condition is used to check if a user input matches a <code>Captcha</code> value
	 * this is designed to work with Captchas contained in <code>net.babeuf.security.Captcha</code>
	 * 
	 * @example
	 * <listing>
	 * var captcha : Captcha = new Captcha(200,150, Captcha.buildD2Dictionary(), ["_sans"] )  ;
	 * captcha.render( 2,"") ; 
	 * 
	 * var input : InputField = new InputField( myField, "captchaInput" ) ;
	 * input.notSent = true ; 
	 * 
	 * var condition : MatchCaptchaCondition = new MatchCaptchaCondition( input, captcha ) ; 
	 * 
	 * trace(condition.match()) ; // outputs true if input value equals captcha code
	 * 
	 * </listing> 
	 * 
	 * @author damien pegoraro 
	 * 
	 * @see net.babeuf.security.Captcha
	 * @see net.babeuf.security.ICaptcha
	 * 
	 * @langversion 3.0
	 * @playerversion Flash 10
	 */
	public class MatchCaptchaCondition extends AbstractCondition implements ICondition
	{
		/**
		 * The <code>InputField</code> containing the user typed value
		 *  
		 * @langversion 3.0
		 * @playerversion Flash 10
		 */
		protected var _field : AbstractField  ;	
		
		/**
		 * The <code>Captcha</code> instance 
		 * 
		 * @langversion 3.0
		 * @playerversion Flash 10 
		 */
		protected var _captcha : ICaptcha ; 
	
		/**
		 * Creates a new instance
		 * 
		 * @param	field  the <code>InputField</code> containing the user typed value
		 * @param	captcha the <code>ICaptcha</code> instance 
		 * 
		 * @see net.babeuf.security.Captcha
		 * @see net.babeuf.security.ICaptcha
		 * 
		 * @langversion 3.0
		 * @playerversion Flash 10
		 */
		public function MatchCaptchaCondition( field : AbstractField, captcha : ICaptcha) 
		{
			_field = field ; 
			_captcha = captcha ; 
		}
		
		/**
		 * @inheritDoc
		 */
		override public function match():Boolean 
		{
			super.match() ; 			
			if ( !_captcha.check(  String(_field.getValue()) )) { _updateSensors(false) ; return false ; }			
			return true ; 
		}
		
		/**
		 * @inheritDoc
		 */
		override public function dispose():void 
		{
			super.dispose();
			_field = null ; 
			_captcha = null ; 
		}
		
		
		
		
	}

}